<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>
            <el-button v-if="isAuth('kernel:user:form')" type="success" icon="Plus" @click="router.push({ name: 'kernel_user_form' })">
              {{ i18n('common.create') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_top">
        <div class="admin_main_block_select">
          <div class="input">
            <el-input v-model="dataForm.nickname" :placeholder="i18n('common.please_input') + i18n('user.nickname')" clearable></el-input>
          </div>
          <div class="input">
            <el-input v-model="dataForm.mobile" :placeholder="i18n('common.please_input') + i18n('user.mobile')" clearable></el-input>
          </div>
          <div class="input">
            <el-date-picker v-model="dataForm.last_login_time" type="daterange" :range-separator="i18n('common.to')" :start-placeholder="i18n('common.start_time')" :end-placeholder="i18n('common.end_time')" clearable></el-date-picker>
          </div>
          <div class="input">
            <el-button icon="Search" @click="getDataList(true)">
              {{ i18n('common.search') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle">
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" width="70">
          </el-table-column>

          <el-table-column :label="i18n('user.user_role_name')" width="100">
            <template #default="scope">
              <span v-if="scope.row.role">
                {{ scope.row.role.title }}
              </span>
            </template>
          </el-table-column>

          <el-table-column prop="nickname" :label="i18n('user.nickname')">
            <template #default="scope">
              <dl class="table_dl">
                <dt>
                  <el-avatar :size="45" :src="scope.row.avatar">
                    <img src="@/assets/images/default/circle.png" />
                  </el-avatar>
                </dt>
                <dd class="table_dl_dd_all_30">
                  {{ i18n('user.nickname') }}： {{ scope.row.nickname }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  {{ i18n('user.create_time') }}： {{ scope.row.create_time }}
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column prop="mobile" :label="i18n('user.mobile')" width="120">
          </el-table-column>

          <el-table-column prop="email" :label="i18n('user.email')" width="180">
          </el-table-column>

          <el-table-column :label="i18n('user.status')" width="80">
            <template #default="scope">
              {{ scope.row.status.text }}
            </template>
          </el-table-column>

          <el-table-column prop="last_login_time" :label="i18n('user.last_login_time')" width="160">
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="430">
            <template #default="scope">
              <el-button v-if="isAuth('kernel:user:form')" type="primary" icon="Edit" @click="router.push({ name: 'kernel_user_form', query: { id: scope.row.id } })">
                {{ i18n('common.update') }}
              </el-button>

              <el-button v-if="isAuth('kernel:user:initialization')" type="warning" icon="Unlock" @click="initializationHandle(scope.row.id)">
                {{ i18n('common.initialization_password') }}
              </el-button>

              <el-button
                v-if="isAuth('kernel:user:status')"
                :type="scope.row.status.value == 2 ? 'danger' : 'success'"
                :icon="scope.row.status.value == 1 ? 'Check' : 'Close'" @click="handleStatus(scope.row.id)">
                <span v-if="scope.row.status.value == 1">
                  {{ i18n('common.enable') }}
                </span>
                <span v-else>
                  {{ i18n('common.disable') }}
                </span>
              </el-button>

              <el-button v-if="isAuth('kernel:user:delete') && 1 != scope.row.id" type="danger" icon="Delete" @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next, jumper, total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="user-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, i18n, isAuth } = useBase()

  const model = ref('user')
  const refresh = inject("refresh")
  const dataForm = reactive([
    'nickname',
    'mobile',
    'last_login_time'
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    handleStatus,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    deleteHandle,
  } = useTable(model.value, dataForm)

  const initializationHandle = (id) => {
    let message = '您确定要初始化当前用户的登录密码？'

    $messagebox.confirm(message, i18n('common.prompt'), {
      confirmButtonText: i18n('common.confirm'),
      cancelButtonText: i18n('common.cancel'),
      type: 'warning'
    }).then(() => {
      $http({
        url: $http.adornUrl('/' + model.value + '/password'),
        method: 'post',
        data: {id: id}
      }).then(({data}) => {
        if (data && data.status === 200) {
          $message.success(i18n('common.handle_success'));
          refresh();
        } else {
          $message.error(i18n(data.message))
        }
      })
    }).catch(() => {})
  }

  onMounted(() => {
    getDataList(model)
  })
</script>
